package com.elitetyc.eliteim.client.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.elitetyc.eliteim.common.constant.Constants;
import com.elitetyc.eliteim.common.model.impl.IMUserInfoImpl;
import com.elitetyc.eliteim.common.proto.RequestProto;
import com.elitetyc.eliteim.common.util.UserInfoHolder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description im客户端配置信息
 * @Date 2021/9/17 9:55
 */
@Configuration
@EnableConfigurationProperties(IMClientProperties.class)
public class IMClientConfig{


    @Autowired
    private IMClientProperties imClientProperties;
    /**
     * 创建单例ping消息
     *
     * @return
     */
    @Bean
    public RequestProto.RequestProtocol ping() {
        return RequestProto.RequestProtocol.newBuilder()
                .setData(Constants.DEFAULT_SUCCESS_MSG)
                .setType(Constants.RequestType.PING)
                .setRId(Constants.DEFAULT_RID).build();
    }

    /**
     * 当未提供连接信息时使用默认的配置信息
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(IMServerInfo.class)
    public IMServerInfo imServerInfo() {
        return new IMServerInfo() {
            @Override
            public String ip() {
                return imClientProperties.getServerIp();
            }

            @Override
            public Integer port() {
                return imClientProperties.getServerPort();
            }

            @Override
            public Integer imPort() {
                return imClientProperties.getServerImPort();
            }
        };
    }

    /**
     * 当前客户端的所有信息
     * @param imServerInfo
     * @return
     */
    @Bean
    public AllInfo allInfo(IMServerInfo imServerInfo){
        final AllInfo allInfo = new AllInfo();
        allInfo.setServer(new AllInfo.ServerInfo(imServerInfo.ip(),imServerInfo.port(),imServerInfo.imPort()));
        return allInfo;
    }



}
